package com.github.soiff.shadow.plugin.helper;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import static com.github.soiff.shadow.plugin.utils.Constants.COMMA;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class RgetHelper implements Helper {
    private final Random random = new Random(System.currentTimeMillis());
    @Override
    public CharSequence apply(Object context, Options options) throws IOException {
        List list;
        if(context instanceof List) {
            list = (List)context;
        } else {
            list = COMMA.splitToList(String.valueOf(context));
        }

        return list.isEmpty()?null:list.get(this.random.nextInt(list.size())).toString();
    }
}
